﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Jemgine.MapEditor.Operations
{
    internal class Rotate : Operation
    {
        List<TransformItem> Items;
        float Delta;
        bool Snap;

        internal Rotate(List<TransformItem> Items, float Delta, bool Snap)
        {
            this.Items = Items;
            this.Delta = Delta;
            this.Snap = Snap;
        }

        internal Rotate(Data.Rotatable Rotatable, float Delta, Object Tag, bool Snap)
        {
            this.Items = new List<TransformItem>();
            this.Items.Add(new TransformItem { Transformee = Rotatable, TransformationTag = Tag });
            this.Delta = Delta;
            this.Snap = Snap;
        }

        internal override ReturnCode Do(EditorState ES)
        {
            foreach (var Item in Items)
            {
                var Rotatable = Item.Transformee as Data.Rotatable;
                if (Rotatable != null)
                {
                    Rotatable.ApplyRotation(Item.TransformationTag, Delta);
                    if (Snap) Rotatable.Snap(Snapper.GetSnapper(ES));
                }
            }
            return ReturnCode.AddToUndoStack;
        }

        internal override void Undo(EditorState ES)
        {
            foreach (var Item in Items)
            {
                var Rotatable = Item.Transformee as Data.Rotatable;
                if (Rotatable != null)
                    Rotatable.ApplyRotation(Item.TransformationTag, 0);

            }
        }
    }
}
